#!/usr/bin/env python
#-*- coding: utf_8 -*-
#
# Copyright 2010 Stanley Cai
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#   http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

__author__= "Stanley Cai"
__email__ = "stanley.w.cai@gmail.com"

import urllib2
import sys
import os

def get_url_content(url):
  count = 3
  while 1:
    try:
      ifile = urllib2.urlopen(url)
      data = ifile.read()
      # stupid tianya uses a mixed charset string
      data = data.decode("gbk", "ignore").encode("utf_8")
      ifile.close()
      break
    except:
      count -= 1
      if count == 0:
        return None
  return data

def get_format(cont):
  str = '<font color="black">共'
  p0 = cont.find(str, 0)
  if p0 < 0:
    return 0
  p1 = cont.find('页', p0)
  if p1 < 0:
    return 0
  return 1

def write_xhtml_head(fp):
  fp.write("""
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 4.0 Transitional//EN">
<html>
<head>
  """)

def write_xhtml_style(fp):
  fp.write("""
<STYLE>
body{font-size:14px;LINE-HEIGHT:26px;MARGIN:0px;background-color: #E7F4FE;}
TABLE{font-size:14px;LINE-HEIGHT:26px;}
.ART{font-size:15px;LINE-HEIGHT:27px;}
.B1{MARGIN:0px;PADDING:0px;LINE-HEIGHT:10px;}
.B2 TABLE  {MARGIN:0px;PADDING:0PX;LINE-HEIGHT:16px;text-align: center;}
.Header{BACKGROUND:#511F90;Color:#FFF;PADDING:5px;Line-HEIGHT:21px;PADDING-LEFT:15px;}
.Header A{text-decoration: None;color:#FFF;}
.Header A:hover{text-decoration: None;color:#f00;}
.FL{float: right;Line-HEIGHT:19px;padding-bottom: 3px;}
.AD{padding-top: 0px;padding-bottom: 10px;margin-top: 0px;}
.mr{font-size:14px;color:#511F90;}
</STYLE>
<meta HTTP-EQUIV="Content-Type" content="text/html; charset=utf-8"> 
  """)

def write_xhtml_body_head(fp):
  fp.write("""
<body>
<TABLE WIDTH=950 height=90 align='CENTER' cellpadding='3' cellspacing='0' >

<TR><TD>
<TABLE WIDTH=90%, ALIGN=CENTER>
</TD>
  """)

def download(url):
  article = get_url_content(url)
  print 'down OK'
  for parser in ["techforum_parser", "publicforum_parser_t0", \
                 "publicforum_parser_t1"]:
    exec "import %s as parser" % parser
    author = parser.get_author(article)
    title = parser.get_title(article)
    if author and title:
      break

  if not author or not title:
    print "this format is not supported. Please send the URL to authors."
    exit()

  print 'author:', author, 'title:', title
  title = title.replace('/','x').replace('\\','x').replace(':','x').replace('*','x').replace('?','x')
  file = title + '.htm'
  if os.path.isfile(file):
    print "File already exists!"
    return
  fp = open(file,'w')
  write_xhtml_head(fp)
  write_xhtml_style(fp)
  write_xhtml_body_head(fp)
  page = 1
  for content in parser.get_pages(article, url):
    res = parser.get_by_author(content, author)
    fp.writelines(['<B>page #%d </B> <br>\n' % page,'<br>'.join(res)])
    print 'page ', page, 'parse ok', len(res)
    fp.flush()
    page += 1
  fp.write("""
</body>
</html>
""")
  fp.close()

def usage(app):
  print "Usage: python %s url [url...]" % app

if __name__=="__main__":
  if len(sys.argv) == 1:
    usage(sys.argv[0])

  for arg in sys.argv[1:]:
    download(arg)
